<template>
    <a-card class="page-main page-main-admin" :bordered="false" v-title data-title="项目周期规则">
   
    <div class="container">
      <div class="hte-head-content">
        <span class="bold-font">项目周期规则</span>
  
      </div>
    <div class="content-admin">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="queryList">
        <a-row :gutter="24">
           <a-col :span="8"  class="hte-col-nowrap">
            <a-form-item label="项目类型"  prop="projectSubType">
              <a-select v-model="queryParam.projectSubType" placeholder="请选择项目类型" :options="projectSubTypeOptions"/>
            </a-form-item>
          </a-col>
         <a-button type="primary" icon="search" @click="queryList">查询</a-button>
         <a-button type="default" style="margin-left:16px" @click="searchReset">重置</a-button>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" v-has="'masterData:projectCycleRules:edit'"  type="primary" icon="plus">新增</a-button>
      <a-button  v-has="'masterData:projectCycleRules:edit'"  type="primary" icon="import" @click="importExcelDataBtn">导入</a-button>
      <a-button type="default" @click="batchDel"  v-has="'masterData:projectCycleRules:del'"><a-icon type="delete"/>删除</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :customHeaderRow="customHeaderRow"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-has="'masterData:projectCycleRules:edit'" >编辑</a>
        </span>
        <span slot="forceCanNotSubmit" slot-scope="text, record">
              {{!!record.forceCanNotSubmit?'是':'否'}}
            </span>

      </a-table>
    </div>
    </div>
    </div>

    <project-cycle-rules-modal ref="modalForm" @ok="reloadTable" @checkRepeat="checkRepeat" :isRepeat="isRepeat" :projectSubTypeOptions="projectSubTypeOptions"></project-cycle-rules-modal>
    <excel-paste :columnsExcel="columnsExcel" :exportXlsTemplateUrl="exportXlsTemplateUrl"  :excelVisible="excelVisible" :excelConfirmLoading="excelConfirmLoading"   @handleOkExcel="handleOkExcel" @handleCancelExcel="handleCancelExcel"></excel-paste>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ProjectCycleRulesModal from './modules/ProjectCycleRulesModal'
 import ExcelPaste from '@/components/excelPaste/ExcelPaste'
   import { httpAction, getAction,postAction } from '@/api/manage'
  export default {
    name: 'ProjectCycleRules',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      ProjectCycleRulesModal,ExcelPaste
    },
    data () {
      return {
        description: '项目周期规则',
        isRepeat:false,
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'项目类型',
            align:"center",
            dataIndex: 'projectSubTypeName'
          },
          {
            title:'项目等级',
            align:"center",
            dataIndex: 'projectLevel'
          },

            {
            title:'开始时间节点',
            align:"center",
            dataIndex: 'startTimeNodeName'
          },
          {
            title:'目标时间节点',
            align:"center",
            dataIndex: 'endTimeNodeName'
          },
          {
            title:'区间描述',
            align:"center",
            dataIndex: 'intervalDescription'
          },
          {
            title:'关系',
            align:"center",
            dataIndex: 'relation'
          },
          {
            title:'时间周期（天）',
            align:"center",
            dataIndex: 'cycleDays'
          },
          {
            title:'强制不能提交',
            align:"center",
            dataIndex: 'forceCanNotSubmit',
            scopedSlots: { customRender: 'forceCanNotSubmit' }
          },
          {
            title:'条件',
            align:"center",
            dataIndex: 'condition'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:100,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/projectCycleRules/list",
          getCodeInfoList: "/manage/codeInfo/queryByType",
          deleteBatch: "/projectCycleRules/deleteBatch",
          exportXlsUrl: "/projectCycleRules/exportXls",
          importExcelUrl: "/projectCycleRules/importExcel",
          addBatch:'/projectCycleRules/addBatch',
        },
        projectSubTypeList:[],
        projectSubTypeOptions:[],
        dictOptions:{},
        superFieldList:[],
         columnsExcel:[
           {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            },
            isImportExcelColumns:false
          },
           {
             title:'项目类型(必填)',
             align:"center",
             dataIndex: 'projectSubType'
           },
           {
             title:'项目等级(必填)',
             align:"center",
             dataIndex: 'projectLevel'
           },

           {
             title:'开始时间节点编号(必填)',
             align:"center",
             dataIndex: 'startTimeNodeCode'
           },
           {
             title:'目标时间节点编号(必填)',
             align:"center",
             dataIndex: 'endTimeNodeCode'
           },
           {
             title:'区间描述',
             align:"center",
             dataIndex: 'intervalDescription'
           },
           {
             title:'关系(必填)',
             align:"center",
             dataIndex: 'relation'
           },
           {
             title:'时间周期（天）(必填)',
             align:"center",
             dataIndex: 'cycleDays'
           },{
             title:'强制不能提交',
             align:"center",
             dataIndex: 'forceCanNotSubmit',
           },
           {
             title:'条件',
             align:"center",
             dataIndex: 'condition'
           },
        ],
        excelVisible:false,
        excelConfirmLoading:false,
        excelDataSource:[],
        exportXlsTemplateUrl:'templates/excel/ProjectCycleRulesTemplate.xlsx'
      }
    },
    created() {
    this.getProjectSubTypeList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      getProjectSubTypeList(){
        const that=this;
        getAction(this.url.getCodeInfoList,{type:"项目子类"}).then(res=>{
          this.projectSubTypeList=res.result||[];
          if(this.projectSubTypeList.length>0){
            this.projectSubTypeList.forEach(function(item){
              that.projectSubTypeOptions.push({value:item.code.trim(),label:item.name.trim()+'('+item.code.trim()+')'});
            })
          }
        })
      },
      queryList(){
        const that=this;
        getAction(this.url.list,{projectSubType:this.queryParam.projectSubType||''}).then(res=>{
          if(res.code === 200){
            this.dataSource=res.result||[];
          }else{
            this.$message.warning('查询异常:'+res.message);
          }
        }).catch(res=>{
          this.excelConfirmLoading = false;
          this.$message.warning('查询异常:'+res.message);

        })
      },
      reloadTable(){
        this.queryList();
        this.onClearSelected();
      },
      checkRepeat(record){
        //暂时取消重复检查
        /*let checkData;
        if(record.id){
          checkData=this.dataSource.filter(d=>{
            return d.id!==record.id&&
              d.projectSubType===record.projectSubType&&
              d.projectLevel===record.projectLevel&&
              d.startTimeNodeCode===record.startTimeNodeCode&&
              d.endTimeNodeCode===record.endTimeNodeCode
          })
        }else{
          checkData=this.dataSource.filter(d=>{
            return d.projectSubType===record.projectSubType&&
              d.projectLevel===record.projectLevel&&
              d.startTimeNodeCode===record.startTimeNodeCode&&
              d.endTimeNodeCode===record.endTimeNodeCode
          })
        }
        if(checkData&&checkData.length>0){
          this.$message.warning('项目类型【'+record.projectSubType+'】项目等级【'+record.projectLevel+'】规则重复，请检查');
          this.isRepeat=true;
        }else{
          this.isRepeat=false;
        }*/
      },
      handleOkExcel:function(data){
        // let allData=JSON.parse(JSON.stringify(this.dataSource));
        for(let i=0;i<data.length;i++){
          let item=data[i];
          if(item.projectSubType==''||item.projectLevel==''||item.startTimeNodeCode==''||item.endTimeNodeCode==''||item.relation==''||item.cycleDays==''){
            this.$message.warning('第 '+(i+1)+' 行，请先把必填项填写完整');
            return;
          }
          // let checkData=allData.filter(d=>{
          //   return d.projectSubType===item.projectSubType&&
          //     d.projectLevel===item.projectLevel&&
          //     d.startTimeNodeCode===item.startTimeNodeCode&&
          //     d.endTimeNodeCode===item.endTimeNodeCode
          // });
          // if(checkData&&checkData.length>0){
          //   this.$message.warning('第 '+(i+1)+' 行，项目类型【'+item.projectSubType+'】项目等级【'+item.projectLevel+'】规则重复，请检查');
          //   return;
          // }
          // allData.push(item);
          item.forceCanNotSubmit=item.forceCanNotSubmit=='是'
        }
        
        this.excelDataSource = data;
        //数据验证
        this.excelConfirmLoading = true;
        postAction(this.url.addBatch,data).then(res => {
          this.excelConfirmLoading = false;
            if(res.code === 200){
              
              this.excelVisible = false;
              this.$message.success("导入成功");
              this.queryList();
            }else{
              this.$message.warning('操作异常:'+res.message);
            }
        }).catch(res=>{
                this.excelConfirmLoading = false;
                this.$message.warning('操作异常:'+res.message);
                
            })
      },
      importExcelDataBtn:function(){
         this.excelVisible = true;
      },
      handleCancelExcel:function(){
        this.excelVisible = false;
       
      },
      customHeaderRow(column, index){
        return {
          on: {
            click: () => {
              this.$message.info('附加字段2(对于时间表固定节点的报工人，填oneAccount),附加字段3(现在用于时间表固定节点的工作完成内容)');
            },     
          }
        }
      }
    }
  }
</script>
<style scoped lang="less">
  @import '~@assets/less/common.less';
  @import '~@assets/less/hte-main.less';
  .table-operator{margin-top: 10px;}
</style>